package com.itheima.controller;

import com.itheima.annotation.LogAnno;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 * @author GY
 * @version V1.0
 * @date 2025-01-08 17:30
 */
@Slf4j
@RestController
@RequestMapping(value = "/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 1.1查询
     */
    @GetMapping
    public Result queryAll(){
        log.info("DeptController queryAll");
        List<Dept> depts = deptService.queryAll();
        return Result.success(depts);
    }

    /**
     * 1.2删除
     */
    @DeleteMapping
    @LogAnno
    public Result delete(Integer id){
        log.info("id:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 1.3新增
     */
    @PostMapping
    @LogAnno
    public Result insert(@RequestBody Dept dept){
        log.info("dept:{}",dept);
        deptService.insert(dept);
        return Result.success();
    }

    /**
     * 1.4修改 : 1 根据ID查询部门
     */
    @GetMapping(value = "/{id}")
    @LogAnno
    public Result queryById(@PathVariable Integer id){
        log.info("id:{}",id);
        Dept dept= deptService.queryById(id);
        return Result.success(dept);
    }
    /**
     * 1.5修改 : 2 修改数据
     */
    @PutMapping
    @LogAnno
    public Result update(@RequestBody Dept dept){
        log.info("dept:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}

